/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.MemberLottery;
import com.boxin.ims.modules.wechat.service.MemberLotteryService;

/**
 * 用户奖品Controller
 * @author XiaoQian
 * @version 2014-05-12
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/memberLottery")
public class MemberLotteryController extends BaseController {

	@Autowired
	private MemberLotteryService memberLotteryService;
	
	@ModelAttribute
	public MemberLottery get(@RequestParam(required=false) Long id) {
		if (id != null){
			return memberLotteryService.get(id);
		}else{
			return new MemberLottery();
		}
	}
	
	@RequiresPermissions("wechat:memberLottery:view")
	@RequestMapping(value = {"list", ""})
	public String list(MemberLottery memberLottery, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		memberLottery.setUser(user);
		if (!user.isAdmin()){
			memberLottery.setUser(user);
		}
        Page<Map<String, Object>> page = memberLotteryService.find(new Page<Map<String, Object>>(request, response), memberLottery); 
        model.addAttribute("page", page);
		return "modules/wechat/memberLotteryList";
	}

	@RequiresPermissions("wechat:memberLottery:view")
	@RequestMapping(value = "form")
	public String form(MemberLottery memberLottery, Model model) {
	
		User user = UserUtils.getUser();
		if (memberLottery.getUser() == null || memberLottery.getUser().getId() <1){
			memberLottery.setUser(user);
		}
		
		model.addAttribute("memberLottery", memberLottery);
		return "modules/wechat/memberLotteryForm";
	}

	@RequiresPermissions("wechat:memberLottery:edit")
	@RequestMapping(value = "save")
	public String save(MemberLottery memberLottery, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, memberLottery)){
			return form(memberLottery, model);
		}
		memberLotteryService.save(memberLottery);
		addMessage(redirectAttributes, "保存用户奖品成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/memberLottery/?repage";
	}
	
	@RequiresPermissions("wechat:memberLottery:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		memberLotteryService.delete(id);
		addMessage(redirectAttributes, "删除用户奖品成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/memberLottery/?repage";
	}
	
	

}
